package com.light.gamecomponents.physics;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.extension.physics.box2d.PhysicsConnector;
import org.anddev.andengine.extension.physics.box2d.PhysicsFactory;
import org.anddev.andengine.extension.physics.box2d.PhysicsWorld;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.light.gamecomponents.CullingSprite;
import com.light.roller.ResourceManager;
import com.light.roller.Roller;
	
public class PhysicsChainPart extends PhysicsComponents{

	static float theDensity=0.0001f;
	public PhysicsChainPart(Roller game, String resourceName, Vector2 initialPosition, Vector2 size) 
	{
		super(game, resourceName, new Vector2(initialPosition), size);
	}
	@Override
	public void onUpdate(float pSecondsElapsed) {
	}

	@Override
	public void reset() {
			
	}

	@Override
	public void onLoadResources() {
		ResourceManager.onLoadResources(m_resourceName, m_game);
		m_textureRegion = ResourceManager.getTextureRegion(m_resourceName);
	}
		
	@Override
	public void unLoadResources() {
		m_textureRegion = null;
		ResourceManager.unLoadResources(m_resourceName, m_game);
	}

	@Override
	public void onLoadScene(Scene scene, PhysicsWorld physicsWorld) {
		FixtureDef objectFixtureDef = PhysicsFactory.createFixtureDef(theDensity, 0.0f, 0.2f);
		theDensity = theDensity+0.0001f;
			
		objectFixtureDef.filter.categoryBits = Roller.CATEGORYBIT_NORMAL;
		objectFixtureDef.filter.maskBits = Roller.MASKBIT_NORMAL;
		
		m_sprite = new CullingSprite(m_initialPosition.x, 
				  		      m_initialPosition.y,
                              m_textureRegion);
			
		m_body = PhysicsFactory.createBoxBody(physicsWorld, 
											  m_sprite, 
											  BodyType.DynamicBody, 
											  objectFixtureDef);

		m_body.setBullet(false);
			
			
		scene.attachChild(m_sprite);
		m_physicsConnector = new PhysicsConnector(m_sprite, m_body, true, true);
		physicsWorld.registerPhysicsConnector(m_physicsConnector);
		getSprite().setColor(0.6f, 0.4f, 0.3f);
			
	}
		
	@Override
	public void removeFromScene(Scene scene, PhysicsWorld physicsWorld) 
	{
		physicsWorld.unregisterPhysicsConnector(m_physicsConnector);
		scene.detachChild(m_sprite);
		physicsWorld.destroyBody(m_body);
			
		//m_physicsConnector = null;
		//m_sprite = null;
		//m_body = null;
	}

	@Override
	public void onLoadEngine() {
			
	}

	@Override
	public void setPosition(int x, int y) {
			// TODO Auto-generated method stub
			
	}
	@Override
	public void rotate(float angle) {
		// TODO Auto-generated method stub
		
	}


}


